﻿using System;
using System.Collections;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Com.HaoJun.Mall.Common
{
    /// <summary>
    /// 字符文本操作类
    /// </summary>
    public class StringHelper
    {
        public static bool IsContains(string[] strs, string value)
        {
            if (strs == null)
            {
                return false;
            }

            foreach (string str in strs)
            {
                if (str == value)
                {
                    return true;
                }
            }

            return false;
        }



        #region 字符串过滤

        #region 对字符串进行HTML编码，针对(input,Textarea)输入时过滤脚本及HTML编码
        /**/
        /// <summary>
        /// 对字符串进行HTML编码，针对(input,Textarea)输入时过滤脚本及HTML编码
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string EncodeToHtml(string source)
        {
            source = source.Trim();
            source = source.Replace("'", "''");
            source = source.Replace("\\", "＼");
            source = HttpContext.Current.Server.HtmlEncode(source);
            source = source.Replace("\r\n", "<br>");
            source = source.Replace("\n", "<br>");
            return source;
        }


        #region [否决的]对字符串进行HTML编码，针对(input,Textarea)输入时过滤脚本及HTML编码
        /**/
        /// <summary>
        /// [否决的]对字符串进行HTML编码，针对(input,Textarea)输入时过滤脚本及HTML编码 (不符合命名规范，请使用 EncodeToHtml 方法 )
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string HtmlFilterForInput(string source)
        {
            return EncodeToHtml(source);
        }


        #region 还原HTML编码为字符串,还原HTML编码为字符串，用于返回到input或 Textarea 输入框
        /**/
        /// <summary>
        /// 还原HTML编码为字符串，用于返回到input或 Textarea 输入框
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string DecodeFormHtml(string source)
        {
            source = source.Trim();
            source = source.Replace("<br>", "\r\n");
            source = source.Replace("<br>", "\n");
            source = HttpContext.Current.Server.HtmlDecode(source);
            return source;
        }


        #region [否决的]还原HTML编码为字符串,还原HTML编码为字符串，用于返回到input或 Textarea 输入框
        /**/
        /// <summary>
        /// [否决的]还原HTML编码为字符串，用于返回到input或 Textarea 输入框 (不符合命名规范，请使用 DecodeFormHtml 方法 )
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string DeHtmlFilterForInput(string source)
        {
            source = source.Trim();
            source = source.Replace("<br>", "\r\n");
            source = source.Replace("<br>", "\n");
            source = HttpContext.Current.Server.HtmlDecode(source);
            return source;
        }


        #region 检验用户提交的URL参数字符里面是否有非法字符
        /**/
        /// <summary>
        /// 检验用户提交的URL参数字符里面是否有非法字符,如果有则返回True.防止SQL注入.
        /// </summary>
        /// <param name="str">(string)</param>
        /// <returns>bool</returns>
        public static bool VerifyString(string str)
        {
            string strTmp = str.ToUpper();
            if (strTmp.IndexOf("SELECT ") >= 0 || strTmp.IndexOf(" AND ") >= 0 || strTmp.IndexOf(" OR ") >= 0 ||
                strTmp.IndexOf("EXEC ") >= 0 || strTmp.IndexOf("CHAR(") >= 0)
            {
                return true;
            }

            strTmp.Replace("'", "＇").Replace(";", "；");
            return false;
        }

        #endregion


        #region 过滤 Sql 语句字符串中的注入脚本
        /**/
        /// <summary>
        /// 过滤 Sql 语句字符串中的注入脚本
        /// </summary>
        /// <param name="source">传入的字符串</param>
        /// <returns></returns>
        #endregion
        public static string FilterSql(string source)
        {
            //单引号替换成两个单引号
            source = source.Replace("'", "''");
            source = source.Replace("\"", "“");
            source = source.Replace("|", "｜");
            //半角封号替换为全角封号，防止多语句执行
            source = source.Replace(";", "；");

            //半角括号替换为全角括号
            source = source.Replace("(", "（");
            source = source.Replace(")", "）");

            /**/
            ///////////////要用正则表达式替换，防止字母大小写得情况////////////////////

            //去除执行存储过程的命令关键字
            source = source.Replace("Exec", "");
            source = source.Replace("Execute", "");

            //去除系统存储过程或扩展存储过程关键字
            source = source.Replace("xp_", "x p_");
            source = source.Replace("sp_", "s p_");

            //防止16进制注入
            source = source.Replace("0x", "0 x");

            return source;
        }


        #region [否决的]过滤 Sql 语句字符串中的注入脚本
        /**/
        /// <summary>
        /// [否决的]过滤 Sql 语句字符串中的注入脚本(请使用 FilterSql 方法 )
        /// </summary>
        /// <param name="source">传入的字符串</param>
        /// <returns></returns>
        #endregion
        public static string SqlFilter(string source)
        {
            return FilterSql(source);
        }


        #region 过滤字符串只剩数字
        /**/
        /// <summary>
        /// 过滤字符串只剩数字
        /// </summary>
        /// <param name="source">源字符串</param>
        #endregion
        public static string FilterToNumber(string source)
        {
            source = Regex.Replace(source, "[^0-9]*", "", RegexOptions.IgnoreCase);
            return source;
        }


        #region [否决的]过滤字符串只剩数字
        /**/
        /// <summary>
        /// [否决的]过滤字符串只剩数字(请使用 FilterToNumber 方法)
        /// </summary>
        /// <param name="source">源字符串</param>
        #endregion
        public static string NumberFilter(string source)
        {
            source = Regex.Replace(source, "[^0-9]*", "", RegexOptions.IgnoreCase);
            return source;
        }


        #region 去除数组内重复元素
        /**/
        /// <summary>
        /// 去除数组内重复元素
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        #endregion
        public ArrayList FilterRepeatArrayItem(ArrayList arr)
        {
            //建立新数组
            ArrayList newArr = new ArrayList();

            //载入第一个原数组元素
            if (arr.Count > 0)
            {
                newArr.Add(arr[0]);
            }

            //循环比较
            for (int i = 0; i < arr.Count; i++)
            {
                if (!newArr.Contains(arr[i]))
                {
                    newArr.Add(arr[i]);
                }
            }
            return newArr;
        }


      


    


        #region 整理(过滤)以英文逗号分割的字符串
        /**/
        /// <summary>
        /// 整理(过滤)以英文逗号分割的字符串
        /// </summary>
        /// <param name="source">原字符串</param>
        /// <param name="str2">待清除的字符串，如空格</param>
        /// <returns></returns>
        #endregion
        public static string FilterStringArray(string source, string str2)
        {
            source = source.Replace(str2, "");
            if (source != "")
            {
                source = source.Replace(",,", ",");

                if (source[0].ToString() == ",")
                {
                    source = source.Substring(1, source.Length - 1);
                }

                if (source[source.Length - 1].ToString() == ",")
                {
                    source = source.Substring(0, source.Length - 1);
                }
            }
            return source;
        }


        #endregion

        #region 字符串组合

        #region 返回年月日时分秒组合的字符串
        /**/
        /// <summary>
        /// 返回年月日时分秒组合的字符串,如：20050424143012 
        /// </summary>
        /// <param name="splitString">中间间隔的字符串，如2005\04\24\14\30\12。可以用来建立目录时使用</param>
        /// <returns></returns>
        #endregion
        public static string GetTimeString()
        {
            //DateTime now = DateTime.Now;

            //StringBuilder sb = new StringBuilder();
            //sb.Append(now.Year.ToString("0000"));
            //sb.Append(splitString);
            //sb.Append(now.Month.ToString("00"));
            //sb.Append(splitString);
            //sb.Append(now.Day.ToString("00"));
            //sb.Append(splitString);
            //sb.Append(now.Hour.ToString("00"));
            //sb.Append(splitString);
            //sb.Append(now.Minute.ToString("00"));
            //sb.Append(splitString);
            //sb.Append(now.Second.ToString("00"));
            string kk = Convert.ToString(DateTime.Now.ToString("d")).Trim().Replace("-", "").Replace("/", "2") + Convert.ToString(DateTime.Now.ToString("T")).Trim().Replace(":", "").Replace(" ", "5");

            return kk;
        }


        #region 返回年月日时分秒组合的字符串
        /**/
        /// <summary>
        /// 返回年月日组合的字符串,如：20050424 (2005年4月24日)
        /// </summary>
        /// <param name="splitString">中间间隔的字符串，如2005\04\24 可以用来建立目录时使用</param>
        /// <returns></returns>
        #endregion
        public static string GetDateString()
        {
            //DateTime now = DateTime.Now;

            //StringBuilder sb = new StringBuilder();
            //sb.Append(now.Year.ToString("0000"));
            //sb.Append(splitString);
            //sb.Append(now.Month.ToString("00"));
            //sb.Append(splitString);
            //sb.Append(now.Day.ToString("00"));
            string kk = Convert.ToString(DateTime.Now.ToString("d")).Trim().Replace("-", "").Replace("/", "2") + Convert.ToString(DateTime.Now.ToString("T")).Trim().Replace(":", "").Replace(" ", "5");
            return kk;
        }


        #endregion

        #region 随机字符串,随机数

        private static string _LowerChar = "abcdefghijklmnopqrstuvwxyz";
        private static string _UpperChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static string _NumberChar = "0123456789";

        #region 获取种子
        /**/
        /// <summary>
        /// 使用RNGCryptoServiceProvider 做种，可以在一秒内产生的随机数重复率非常
        /// 的低，对于以往使用时间做种的方法是个升级
        /// </summary>
        /// <returns></returns>
        #endregion
        public static int GetNewSeed()
        {
            byte[] rndBytes = new byte[4];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(rndBytes);
            return BitConverter.ToInt32(rndBytes, 0);
        }


        #region 取得指定范围内的数字随几数
        /**/
        /// <summary>
        /// 取得指定范围内的随几数
        /// </summary>
        /// <param name="startNumber">下限数</param>
        /// <param name="endNumber">上限数</param>
        /// <returns>int</returns>
        #endregion
        public static int GetRandomNumber(int startNumber, int endNumber)
        {
            Random objRandom = new Random(GetNewSeed());
            int r = objRandom.Next(startNumber, endNumber);
            return r;
        }


        #region 获取指定 ASCII 范围内的随机字符串
        /**/
        /// <summary>
        /// 获取指定 ASCII 范围内的随机字符串
        /// </summary>
        /// <param name="resultLength">结果字符串长度</param>
        /// <param name="startNumber"> 开始的ASCII值 如（33－125）中的 33</param>
        /// <param name="endNumber"> 结束的ASCII值 如（33－125）中的 125</param>
        /// <returns></returns>
        #endregion
        public static string GetRandomStringByASCII(int resultLength, int startNumber, int endNumber)
        {
            Random objRandom = new Random(GetNewSeed());
            string result = null;
            for (int i = 0; i < resultLength; i++)
            {
                result += (char)objRandom.Next(startNumber, endNumber);
            }
            return result;
        }


        #region 从指定字符串中抽取指定长度的随机字符串
        /**/
        /// <summary>
        /// 从指定字符串中抽取指定长度的随机字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="resultLength">待获取随机字符串长度</param>
        /// <returns></returns>
        #endregion
        private static string GetRandomString(string source, int resultLength)
        {
            Random objRandom = new Random(GetNewSeed());
            string result = null;
            for (int i = 0; i < resultLength; i++)
            {
                result += source.Substring(objRandom.Next(0, source.Length - 1), 1);
            }
            return result;
        }


        #region 获取指定长度随机的数字字符串
        /**/
        /// <summary>
        /// 获取指定长度随机的数字字符串
        /// </summary>
        /// <param name="resultLength">待获取随机字符串长度</param>
        /// <returns></returns>
        #endregion
        public static string GetRandomNumberString(int resultLength)
        {
            return GetRandomString(_NumberChar, resultLength);
        }


        #region 获取指定长度随机的字母字符串（包含大小写字母）
        /**/
        /// <summary>
        /// 获取指定长度随机的字母字符串（包含大小写字母）
        /// </summary>
        /// <param name="resultLength">待获取随机字符串长度</param>
        /// <returns></returns>
        #endregion
        public static string GetRandomLetterString(int resultLength)
        {
            return GetRandomString(_LowerChar + _UpperChar, resultLength);
        }


        #region 获取指定长度随机的字母＋数字混和字符串（包含大小写字母）
        /**/
        /// <summary>
        /// 获取指定长度随机的字母＋数字混和字符串（包含大小写字母）
        /// </summary>
        /// <param name="resultLength">待获取随机字符串长度</param>
        /// <returns></returns>
        #endregion
        public static string GetRandomMixString(int resultLength)
        {
            return GetRandomString(_LowerChar + _UpperChar + _NumberChar, resultLength);
        }

        #endregion

        #region 字符串验证

        #region 判断字符串是否为整型
        /**/
        /// <summary>
        /// 判断字符串是否为整型
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static bool IsInteger(string source)
        {
            if (source == null || source == "")
            {
                return false;
            }

            if (Regex.IsMatch(source, "^((\\+)\\d)?\\d*$"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        #region Email 格式是否合法
        /**/
        /// <summary>
        /// Email 格式是否合法
        /// </summary>
        /// <param name="strEmail"></param>
        #endregion
        public static bool IsEmail(string strEmail)
        {
            return Regex.IsMatch(strEmail, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
        }


        #region 判断是否IP
        /**/
        /// <summary>
        /// 判断是否IP
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static bool IsIP(string source)
        {
            return Regex.IsMatch(source, @"^(((25[0-5]|2[0-4][0-9]|19[0-1]|19[3-9]|18[0-9]|17[0-1]|17[3-9]|1[0-6][0-9]|1[1-9]|[2-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|(192\.(25[0-5]|2[0-4][0-9]|16[0-7]|169|1[0-5][0-9]|1[7-9][0-9]|[1-9][0-9]|[0-9]))|(172\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|1[0-5]|3[2-9]|[4-9][0-9]|[0-9])))\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$");
        }


        #region 检查字符串是否为A-Z、0-9及下划线以内的字符

        /**/
        /// <summary>
        /// 检查字符串是否为A-Z、0-9及下划线以内的字符
        /// </summary>
        /// <param name="str">被检查的字符串</param>
        /// <returns>是否有特殊字符</returns>
        #endregion
        public static bool IsLetterOrNumber(string str)
        {
            bool b = Regex.IsMatch(str, "[a-zA-Z0-9_]");
            return b;
        }


        #region 验输入字符串是否含有“/\<>:.?*|$]”特殊字符
        /**/
        /// <summary>
        /// 验输入字符串是否含有“/\:.?*|$]”特殊字符
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static bool IsSpecialChar(string source)
        {
            Regex r = new Regex(@"[/\<>:.?*|$]");
            return r.IsMatch(source);
        }


        #region 是否全为中文/日文/韩文字符
        /**/
        /// <summary>
        /// 是否全为中文/日文/韩文字符
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <returns></returns>
        #endregion
        public static bool IsChineseChar(string source)
        {
            //中文/日文/韩文: [\u4E00-\u9FA5]
            //英文:[a-zA-Z]
            return Regex.IsMatch(source, @"^[\u4E00-\u9FA5]+$");
        }


        #region 是否包含双字节字符(允许有单字节字符)
        /**/
        /// <summary>
        /// 是否包含双字节字符(允许有单字节字符)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static bool IsDoubleChar(string source)
        {
            return Regex.IsMatch(source, @"[^\x00-\xff]");
        }


        #region 是否为日期型字符串
        /**/
        /// <summary>
        /// 是否为日期型字符串
        /// </summary>
        /// <param name="source">日期字符串(2005-6-30)</param>
        /// <returns></returns>
        #endregion
        public static bool IsDate(string source)
        {
            return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
        }


        #region 是否为时间型字符串
        /**/
        /// <summary>
        /// 是否为时间型字符串
        /// </summary>
        /// <param name="source">时间字符串(15:00:00)</param>
        /// <returns></returns>
        #endregion
        public static bool IsTime(string source)
        {
            return Regex.IsMatch(source, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }


        #region 是否为日期+时间型字符串
        /**/
        /// <summary>
        /// 是否为日期+时间型字符串
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static bool IsDateTime(string source)
        {
            return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) ((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }


        #region 是否为钱币型数据
        /**/
        /// <summary>
        /// 是否为钱币型数据
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static bool IsMoney(string source)
        {
            return false;
        }

        #endregion

        #region 字符串截取

        #region 获取字符串的实际长度(按单字节)
        /**/
        /// <summary>
        /// 获取字符串的实际长度(按单字节)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static int GetRealLength(string source)
        {
            return Encoding.Default.GetByteCount(source);
        }


        #region 取得固定长度的字符串(按单字节截取)
        /**/
        /// <summary>
        /// 取得固定长度的字符串(按单字节截取)。
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="resultLength">截取长度</param>
        /// <returns></returns>
        #endregion
        public static string SubString(string source, int resultLength)
        {

            //判断字符串长度是否大于截断长度
            if (Encoding.Default.GetByteCount(source) > resultLength)
            {
                //判断字串是否为空
                if (source == null)
                {
                    return "";
                }

                //初始化
                int i = 0, j = 0;

                //为汉字或全脚符号长度加2否则加1
                foreach (char newChar in source)
                {
                    if ((int)newChar > 127)
                    {
                        i += 2;
                    }
                    else
                    {
                        i++;
                    }
                    if (i > resultLength)
                    {
                        source = source.Substring(0, j);
                        break;
                    }
                    j++;
                }
            }
            return source;
        }


        #region 按长度分割字符串
        /**/
        /// <summary>
        /// 按长度分割字符串，如短信
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        #endregion
        private ArrayList SplitStringByLength(string str, int len)
        {
            ArrayList arrBlock = new ArrayList();
            int intBlockCount = str.Length / len;
            if (str.Length % len != 0)
            {
                for (int i = 0; i <= intBlockCount; i++)
                {
                    if ((str.Length - i * len) > len)
                        arrBlock.Add(str.Substring(i * len, len));
                    else
                        arrBlock.Add(str.Substring(i * len, (str.Length % len)));
                }
            }
            else
            {
                for (int i = 0; i < intBlockCount; i++)
                {
                    arrBlock.Add(str.Substring(i * len, len));
                }
            }
            return arrBlock;
        }


        #endregion

        #region 字符串比较

        /**/
        /// <summary>
        ///  获得某个字符串在另个字符串中出现的次数
        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strSymbol">符号</param>
        /// <returns>返回值</returns>
        public static int GetStringIncludeCount(string strOriginal, string strSymbol)
        {

            int count = 0;
            count = strOriginal.Length - strOriginal.Replace(strSymbol, String.Empty).Length;
            return count;
        }

        /**/
        /// <summary>
        /// 获得某个字符串在另个字符串第一次出现时前面所有字符
        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strSymbol">符号</param>
        /// <returns>返回值</returns>
        public static string GetFirstString(string strOriginal, string strSymbol)
        {
            int strPlace = strOriginal.IndexOf(strSymbol);
            if (strPlace != -1)
            {
                strOriginal = strOriginal.Substring(0, strPlace);
            }
            return strOriginal;
        }

        /**/
        /// <summary>
        /// 获得某个字符串在另个字符串最后一次出现时后面所有字符
        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strSymbol">符号</param>
        /// <returns>返回值</returns>
        public static string GetLastString(string strOriginal, string strSymbol)
        {
            int strPlace = strOriginal.LastIndexOf(strSymbol) + strSymbol.Length;
            strOriginal = strOriginal.Substring(strPlace);
            return strOriginal;
        }

        /**/
        /// <summary>
        /// 获得两个字符之间第一次出现时前面所有字符
        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strFirst">最前哪个字符</param>
        /// <param name="strLast">最后哪个字符</param>
        /// <returns>返回值</returns>
        public static string GetTwoMiddleFirstStr(string strOriginal, string strFirst, string strLast)
        {
            strOriginal = GetFirstString(strOriginal, strLast);
            strOriginal = GetLastString(strOriginal, strFirst);
            return strOriginal;
        }

        /**/
        /// <summary>
        ///  获得两个字符之间最后一次出现时的所有字符
        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strFirst">最前哪个字符</param>
        /// <param name="strLast">最后哪个字符</param>
        /// <returns>返回值</returns>
        public static string GetTwoMiddleLastStr(string strOriginal, string strFirst, string strLast)
        {
            strOriginal = GetLastString(strOriginal, strFirst);
            strOriginal = GetFirstString(strOriginal, strLast);
            return strOriginal;
        }

        /**/
        /// <summary>
        /// 发帖过滤词（用“|”号分隔）Application["app_state_FilterWord"]
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="chkword">过滤词（用“|”号分隔）</param>
        public static bool CheckBadWords(string str, string chkword)
        {
            if (chkword != null && chkword != "")
            {
                string filter = chkword;
                string chk1 = "";
                string[] aryfilter = filter.Split('|');
                for (int i = 0; i < aryfilter.Length; i++)
                {
                    chk1 = aryfilter[i].ToString();
                    if (str.IndexOf(chk1) >= 0)
                        return true;
                }
            }
            return false;
        }

        /**/
        /// <summary>
        /// 发帖过滤字(需审核)（不同组间用“§”号分隔，同组内用“,”分隔）Application["app_state_Check_FilterWord"]
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="chkword">过滤字(需审核)（不同组间用“§”号分隔，同组内用“,”分隔）</param>
        public static bool CheckilterStr(string str, string chkword)
        {

            if (chkword != null && chkword != "")
            {
                string filter = chkword;
                string[] aryfilter = filter.Split('§');
                string[] aryfilter_lei;
                int lei_for = 0, j;

                for (int i = 0; i < aryfilter.Length; i++)
                {
                    lei_for = 0;
                    aryfilter_lei = aryfilter[i].Split(',');
                    for (j = 0; j < aryfilter_lei.Length; j++)
                    {
                        if (str.IndexOf(aryfilter_lei[j].ToString()) >= 0)
                            lei_for += 1;
                    }
                    if (lei_for == aryfilter_lei.Length)
                        return true;
                }
            }
            return false;
        }


        #endregion

        #region 字符集转换

        #region 将 GB2312 值转换为 UTF8 字符串(如：测试 -> 娴嬭瘯 )
        /**/
        /// <summary>
        /// 将 GB2312 值转换为 UTF8 字符串(如：测试 -> 娴嬭瘯 )
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string ConvertGBToUTF8(string source)
        {
            /**/
            /*
        byte[] bytes;
        bytes = System.Text.Encoding.Default.GetBytes(source);
        System.Text.Decoder gbDecoder = System.Text.Encoding.GetEncoding("gb2312").GetDecoder();
        int charCount = gbDecoder.GetCharCount(bytes,0 , bytes.Length);
        Char[] chars = new Char[charCount];
        int charsDecodedCount = gbDecoder.GetChars(bytes,0,bytes.Length,chars,0);
        return new string(chars);
        */

            return Encoding.GetEncoding("GB2312").GetString(Encoding.UTF8.GetBytes(source));
        }


        #region 将 UTF8 值转换为 GB2312 字符串 (如：娴嬭瘯 -> 测试)
        /**/
        /// <summary>
        /// 将 UTF8 值转换为 GB2312 字符串 (如：娴嬭瘯 -> 测试)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string ConvertUTF8ToGB(string source)
        {
            /**/
            /*
        byte[] bytes;
        //模拟编码
        System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
        bytes = utf8.GetBytes(source); 
            
        //开始解码
        System.Text.Decoder utf8Decoder = System.Text.Encoding.UTF8.GetDecoder();

        int charCount = utf8Decoder.GetCharCount(bytes, 0, bytes.Length);
        Char[] chars = new Char[charCount];
        int charsDecodedCount = utf8Decoder.GetChars(bytes, 0, bytes.Length, chars, 0);

        return new string(chars);
        */

            return Encoding.UTF8.GetString(Encoding.GetEncoding("GB2312").GetBytes(source));
        }


        #region 由16进制转为汉字字符串（如：B2E2 -> 测 ）
        /**/
        /// <summary>
        /// 由16进制转为汉字字符串（如：B2E2 -> 测 ）
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string ConvertHexToString(string source)
        {
            byte[] oribyte = new byte[source.Length / 2];
            for (int i = 0; i < source.Length; i += 2)
            {
                string str = Convert.ToInt32(source.Substring(i, 2), 16).ToString();
                oribyte[i / 2] = Convert.ToByte(source.Substring(i, 2), 16);
            }
            return Encoding.Default.GetString(oribyte);
        }


        #region  字符串转为16进制字符串（如：测 -> B2E2 ）
        /**/
        /// <summary>
        /// 字符串转为16进制字符串（如：测 -> B2E2 ）
        /// </summary>
        /// <param name="Word"></param>
        /// <returns></returns>
        #endregion
        public static string ConvertToHex(string Word)
        {
            int i = Word.Length;
            string temp;
            string end = "";
            byte[] array = new byte[2];
            int i1, i2;
            for (int j = 0; j < i; j++)
            {
                temp = Word.Substring(j, 1);
                array = Encoding.Default.GetBytes(temp);
                if (array.Length.ToString() == "1")
                {
                    i1 = Convert.ToInt32(array[0]);
                    end += Convert.ToString(i1, 16);
                }
                else
                {
                    i1 = Convert.ToInt32(array[0]);
                    i2 = Convert.ToInt32(array[1]);
                    end += Convert.ToString(i1, 16);
                    end += Convert.ToString(i2, 16);
                }
            }
            return end.ToUpper();
        }


        #region 字符串转为unicode字符串（如：测试 -> 测试）
        /**/
        /// <summary>
        /// 字符串转为unicode字符串（如：测试 -> 测试）
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string ConvertToUnicode(string source)
        {
            StringBuilder sa = new StringBuilder();//Unicode
            string s1;
            string s2;
            for (int i = 0; i < source.Length; i++)
            {
                byte[] bt = Encoding.Unicode.GetBytes(source.Substring(i, 1));
                if (bt.Length > 1)//判断是否汉字
                {
                    s1 = Convert.ToString((short)(bt[1] - '\0'), 16);//转化为16进制字符串
                    s2 = Convert.ToString((short)(bt[0] - '\0'), 16);//转化为16进制字符串
                    s1 = (s1.Length == 1 ? "0" : "") + s1;//不足位补0
                    s2 = (s2.Length == 1 ? "0" : "") + s2;//不足位补0
                    sa.Append("&#" + Convert.ToInt32(s1 + s2, 16) + ";");
                }
            }

            return sa.ToString();
        }


        #region 字符串转为UTF8字符串（如：测试 -> \u6d4b\u8bd5）
        /**/
        /// <summary>
        /// 字符串转为UTF8字符串（如：测试 -> \u6d4b\u8bd5）
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string ConvertToUTF8(string source)
        {
            StringBuilder sb = new StringBuilder();//UTF8
            string s1;
            string s2;
            for (int i = 0; i < source.Length; i++)
            {
                byte[] bt = Encoding.Unicode.GetBytes(source.Substring(i, 1));
                if (bt.Length > 1)//判断是否汉字
                {
                    s1 = Convert.ToString((short)(bt[1] - '\0'), 16);//转化为16进制字符串
                    s2 = Convert.ToString((short)(bt[0] - '\0'), 16);//转化为16进制字符串
                    s1 = (s1.Length == 1 ? "0" : "") + s1;//不足位补0
                    s2 = (s2.Length == 1 ? "0" : "") + s2;//不足位补0
                    sb.Append("\\u" + s1 + s2);
                }
            }

            return sb.ToString();
        }


        #region 转化为ASC码方法
        //转化为ASC码方法

        public string ConvertToAsc(string txt)
        {
            string newtxt = "";
            foreach (char c in txt)
            {

                newtxt += Convert.ToString((int)c);
            }
            return newtxt;

        }
        #endregion

        #region BASE64

        /**/
        /// <summary>
        /// 将字符串使用base64算法加密
        /// </summary>
        /// <param name="source">待加密的字符串</param>
        /// <returns>加码后的文本字符串</returns>
        public static string Base64_Encrypt(string source)
        {
            return Convert.ToBase64String(Encoding.Default.GetBytes(source));
        }

        /**/
        /// <summary>
        /// 从Base64编码的字符串中还原字符串，支持中文
        /// </summary>
        /// <param name="source">Base64加密后的字符串</param>
        /// <returns>还原后的文本字符串</returns>
        public static string Base64_Decrypt(string source)
        {
            return Encoding.Default.GetString(Convert.FromBase64String(source));
        }
        #endregion

        #endregion

        #region 字符串格式化

        #region 将字符串反转
        /**/
        /// <summary>
        /// 将字符串反转
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        #endregion
        public static string FormatReverse(string source)
        {
            char[] ca = source.ToCharArray();
            Array.Reverse(ca);
            source = new String(ca);
            return source;
        }


        /**/
        /// <summary>
        /// 格式化为小数点两位的字符串(四舍五入)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string FormatFloat2(double source)
        {
            //double tmp=1223333.24215265652;
            return source.ToString("##,###.000");
        }


        /**/
        /// <summary>
        /// 将数字字符串转成货币格式
        /// </summary>
        /// <returns></returns>
        public static string GetMoneyFormat()
        {
            /**/
            /*
        string.Format("${0:###,###,###.##}元",decimalVar);
        --------------------------------------------------------------------------------
        找到system.string的format方法
        string str="99988"
        str=format(str,"##,####.00")
        str value is:99,988.00
        试一下行不行
        --------------------------------------------------------------------------------
        string strMoney="544.54";
        decimal decMoney=System.Convert.ToDecimal(strMoney);
        string money=decMoney.ToString("c");

        如果要转换成不同地区的符号，可以用：
        CultureInfo MyCulture =  new CultureInfo("zh-CN");
        String MyString = MyInt.ToString("C", MyCulture);

        注意:这里OrderTotal的格式只能是Decimal.

         */
            return "";
        }


        /**/
        /// <summary>
        /// 格式化字符串 字符串1234567,想变成1,234,567
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetO1(string source)
        {
            //return source.ToString("N");
            //return    source.ToString("###,###");
            return "";
        }


        /**/
        /// <summary>
        /// 十进制数字1转换为字符串0a，在前面补零
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string OctToHex(int source)
        {
            return source.ToString("X2");
        }


        /**/
        /// <summary>
        /// 将一个十六进制表示的字符串转成int型？如"0A" = 10
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string HexToOct(string source)
        {
            return Convert.ToInt16(source, 16).ToString();
        }


        #endregion

        #region 身份证验证

        #region 从18位身份证获取用户所在省份、生日、性别信息
        /**/
        /// <summary>
        /// 从18位身份证获取用户所在省份、生日、性别信息
        /// </summary>
        /// <param name="cid">身份证字符串</param>
        /// <returns>如：福建,1978-06-30,男</returns>
        #endregion
        public static string GetCidInfo(string cid)
        {
            string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃", "青海", "宁夏", "***", null, null, null, null, null, "台湾", null, null, null, null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null, null, null, null, "国外" };
            double iSum = 0;
            Regex rg = new Regex(@"^\d{17}(\d|x)$");
            Match mc = rg.Match(cid);
            if (!mc.Success)
            {
                return "";
            }
            cid = cid.ToLower();
            cid = cid.Replace("x", "a");
            if (aCity[int.Parse(cid.Substring(0, 2))] == null)
            {
                return "非法地区";
            }
            try
            {
                DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
            }
            catch
            {
                return "非法生日";
            }
            for (int i = 17; i >= 0; i--)
            {
                iSum += (Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), NumberStyles.HexNumber);

            }
            if (iSum % 11 != 1)
                return ("非法证号");

            return (aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + (int.Parse(cid.Substring(16, 1)) % 2 == 1 ? "男" : "女"));

        }


        #region 十五位的身份证号转为十八位的
        /**/
        /// <summary>
        /// 十五位的身份证号转为十八位的
        /// </summary>
        /// <param name="source">十五位的身份证号</param>
        /// <returns></returns>
        #endregion
        public static string Cid15To18(string source)
        {
            string[] arrInt = new string[17] { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" };
            string[] arrCh = new string[11] { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };

            int nTemp = 0, i;

            if (source.Length == 15)
            {
                source = source.Substring(0, 6) + "19" + source.Substring(6, source.Length - 6);
                for (i = 0; i < source.Length; i++)
                {
                    nTemp += int.Parse(source.Substring(i, 1)) * int.Parse(arrInt[i]);
                }
                source += arrCh[nTemp % 11];

                return source;
            }
            else
            {
                return null;
            }

        }
        #endregion

        #region 随机数
        public static string GetRandNum(int randNumLength)
        {
            Random randNum = new Random(unchecked((int)DateTime.Now.Ticks));
            StringBuilder sb = new StringBuilder(randNumLength);
            for (int i = 0; i < randNumLength; i++)
            {
                sb.Append(randNum.Next(0, 9));
            }
            return sb.ToString();
        }

        #endregion

        #region 过滤HTML代码的函数
        /// <summary>
        /// 过滤HTML代码的函数
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public string HtmlStr(string html)
        {
            Regex regex1 = new Regex(@"<script[\s\S]+</script *>", RegexOptions.IgnoreCase);
            Regex regex2 = new Regex(@" href *= *[\s\S]*script *:", RegexOptions.IgnoreCase);
            Regex regex3 = new Regex(@" no[\s\S]*=", RegexOptions.IgnoreCase);
            Regex regex4 = new Regex(@"<iframe[\s\S]+</iframe *>", RegexOptions.IgnoreCase);
            Regex regex5 = new Regex(@"<frameset[\s\S]+</frameset *>", RegexOptions.IgnoreCase);
            Regex regex6 = new Regex(@"\<img[^\>]+\>", RegexOptions.IgnoreCase);
            Regex regex7 = new Regex(@"</p>", RegexOptions.IgnoreCase);
            Regex regex8 = new Regex(@"<p>", RegexOptions.IgnoreCase);
            Regex regex9 = new Regex(@"<[^>]*>", RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 


            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
            html = regex4.Replace(html, ""); //过滤iframe 
            html = regex5.Replace(html, ""); //过滤frameset 
            html = regex6.Replace(html, ""); //过滤frameset 
            html = regex7.Replace(html, ""); //过滤frameset 
            html = regex8.Replace(html, ""); //过滤frameset 
            html = regex9.Replace(html, "");
            html = html.Replace(" ", "");


            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            return html;
        }
        #endregion


    }
}